namespace Vibstudio.X4NET.Xml.Generation
{
    internal class InstanceGroup : InstanceObject
    {
        #region Fields

        private InstanceGroup _child;
        private decimal _occurs = 1;

        #endregion

        #region Properties

        internal InstanceGroup Child
        {
            get { return _child; }
            set { _child = value; }
        }

        internal bool IsChoice { get; set; }

        internal int NoOfChildren
        {
            get
            {
                int cnt = 0;
                InstanceGroup currentGroup = _child;
                while (currentGroup != null)
                {
                    cnt++;
                    currentGroup = currentGroup.Sibling;
                }
                return cnt;
            }
        }

        internal decimal Occurs
        {
            get { return _occurs; }

            set { _occurs = value; }
        }

        internal InstanceGroup Parent { get; set; }

        internal InstanceGroup Sibling { get; set; }

        #endregion

        #region Methods

        internal void AddChild(InstanceGroup obj)
        {
            obj.Parent = this;
            if (_child == null)
            {
                _child = obj;
            }
            else
            {
                InstanceGroup prev = null;
                InstanceGroup next = _child;
                while (next != null)
                {
                    prev = next;
                    next = next.Sibling;
                }
                if (prev != null)
                {
                    prev.Sibling = obj;
                }
            }
        }

        internal InstanceGroup GetChild(int index)
        {
            int curIndex = 0;
            InstanceGroup currentGroup = _child;
            while (currentGroup != null)
            {
                if (curIndex == index)
                {
                    return currentGroup;
                }
                curIndex++;
                currentGroup = currentGroup.Sibling;
            }
            return null;
        }

        #endregion
    }
}